<script setup>
import { User, Lock } from "@element-plus/icons-vue";
import { ref } from "vue";
import { reactive } from "vue";
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();

//控制注册与登录表单的显示， 默认显示注册
const isRegister = ref(false);
// 在组件挂载时获取路由参数
const type = route.params.type;
if (type === "register") {
  isRegister.value = true;
}
// 定义数据模型
const registerData = ref({
  userName: "",
  password: "",
  rePassword: "",
});

const rules = {
  userName: [
    { required: true, message: "用户名不能为空", trigger: "blur" },
    { min: 3, max: 16, message: "3~16位非空字符", trigger: "blur" },
  ],
  nickName: [{ required: true, message: "昵称不能为空", trigger: "blur" }],
  password: [
    { required: true, message: "密码不能为空", trigger: "blur" },
    { min: 5, max: 16, message: "5~16位非空字符", trigger: "blur" },
  ],
  rePassword: [
    {
      validator: (rule, value, callback) => {
        if (value !== registerData.value.password) {
          return callback(new Error("两次密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
};
import { userRegisterService } from "@/api/user";
import { ElMessage } from "element-plus";
const register = async () => {
  if (registerData.value.password !== registerData.value.rePassword) {
    return ElMessage.error("两次密码不一致");
  }
  let res = await userRegisterService(registerData.value);
  res.code == 1 ? ElMessage.success("注册成功!") : ElMessage.error("注册失败!");
};

import { userLoginService } from "@/api/user";
import { useTokenStore,useBankInfoStore } from "@/stores/token.js";

const tokenStore = useTokenStore();

// 登录
const login = async () => {
  let res = await userLoginService(registerData.value);
  console.log(res.data);
  
  // 存入token
  tokenStore.setToken(res.data.token);
  useBankInfoStore().setBankInfo(res.data.bankName)
  useBankInfoStore().setBankId(res.data.bankId)
  console.log(res.data.bankName);
  console.log(res.data.bankId);
  res.code == 0 ? ElMessage.success("登录成功!") : ElMessage.error("登录失败!");
  router.push("/");
};
// 清空输入框
const clearInput = () => {
  registerData.value.userName = "";
  registerData.value.password = "";
};
</script>

<template>
  <div class="container">
    <!-- 注册表单 -->
    <el-form
      ref="form"
      size="large"
      autocomplete="off"
      v-if="isRegister"
      :model="registerData"
      :rules="rules"
    >
      <el-form-item>
        <h1>注册</h1>
      </el-form-item>
      <el-form-item prop="userName">
        <el-input
          :prefix-icon="User"
          placeholder="请输入用户名"
          v-model="registerData.userName"
        ></el-input>
      </el-form-item>
      <el-form-item prop="nickName">
        <el-input
          :prefix-icon="User"
          placeholder="请输入昵称"
          v-model="registerData.nickName"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          :prefix-icon="Lock"
          type="password"
          placeholder="请输入密码"
          v-model="registerData.password"
        ></el-input>
      </el-form-item>
      <el-form-item prop="rePassword">
        <el-input
          :prefix-icon="Lock"
          type="password"
          placeholder="请输入再次密码"
          v-model="registerData.rePassword"
        ></el-input>
      </el-form-item>
      <!-- 注册按钮 -->
      <el-form-item>
        <el-button
          class="button"
          type="primary"
          auto-insert-space
          @click="register"
          >注册</el-button
        >
      </el-form-item>
      <el-form-item class="flex">
        <el-link type="info" :underline="false" @click="isRegister = false"
          >← 返回</el-link
        >
      </el-form-item>
    </el-form>
    <!-- 登录表单 -->
    <el-form
      ref="form"
      size="large"
      autocomplete="off"
      v-else
      :model="registerData"
      :rules="rules"
    >
      <el-form-item class="title">
        <h1>登录</h1>
      </el-form-item>
      <el-form-item prop="userName">
        <el-input
          :prefix-icon="User"
          placeholder="请输入用户名"
          v-model="registerData.userName"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          name="password"
          :prefix-icon="Lock"
          type="password"
          placeholder="请输入密码"
          v-model="registerData.password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <div class="flex">
          <el-checkbox>记住我</el-checkbox>
          <el-link type="primary" :underline="false">忘记密码？</el-link>
        </div>
      </el-form-item>
      <!-- 登录按钮 -->
      <el-form-item>
        <el-button
          class="button"
          type="primary"
          auto-insert-space
          @click="login"
          >登录</el-button
        >
      </el-form-item>
      <el-form-item class="flex">
        <el-link
          type="info"
          :underline="false"
          @click="
            isRegister = true;
            clearInput();
          "
          >注册 →</el-link
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="scss" scoped>
/* 样式 */
.container {
  background-image: url("@/assets/img/rice.png");
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
}
.el-form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  background-color: #fff;
  user-select: none;
  width: 100%;
  max-width: 400px;
  padding: 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  h1 {
    text-align: center;
    margin-bottom: 20px;
    width: 100%; /* 确保标题宽度占满整个表单 */
  }
  .el-form-item {
    margin-bottom: 15px;
  }
  .el-input {
    width: 100%;
  }
  .button {
    width: 100%;
    margin-top: 10px;
  }
  .flex {
    display: flex;
    justify-content: space-between; /* 确保元素分别靠左和靠右对齐 */
    align-items: center;
    margin-bottom: 10px; /* 增加底部间距 */
    .el-checkbox {
      margin-right: 10px; /* 增加复选框与链接的间距 */
    }
    .el-link {
      font-size: 0.9em;
    }
  }
}
@media (max-width: 768px) {
  .container {
    padding: 10px;
  }
  .el-form {
    max-width: 100%;
    padding: 15px;
  }
  .flex {
    flex-direction: column; /* 在小屏幕上改为垂直布局 */
    align-items: flex-start; /* 确保元素左对齐 */
    .el-checkbox {
      margin-bottom: 5px; /* 增加复选框与链接的间距 */
    }
    .el-link {
      margin-left: 0; /* 确保链接与复选框对齐 */
    }
  }
}
</style>